<?php
/**
 * Dirox Framework
 * Copyright (C) 2005 - 2007 Dirox / Synexser
 * 
 * All rights reserved.
 */

include ("class.html_output.php5");

class HTMLInput {

	function HTMLInput() {
    }

	function text($label = "", $name = "", $id = "", $value = "", $size = "", $maxlength = "", $require = false, $type = "text", $comment = "") {
		$output = new HTMLoutput();

		if (!empty($name))
			$name = "name=\"$name\"";
		if (!empty($id))
			$id = "id=\"$id\"";
		if (!empty($size))
			$size = "size=\"$size\"";
		if (!empty($maxlength))
			$maxlength = "maxlength=\"$maxlength\"";
		if ($require)
			$label .= "&nbsp;<font color=\"#FF0000\">*</font>";
		if (!empty($comment))
			$comment = "<br />$comment";
		$value = $output->htmlencode($value);

		$label .= "&nbsp;:&nbsp;";


		return "<tr>
	  	<th>$label</th>
	  	<td>
	    <input type=\"$type\" $name $id value=\"$value\" $size $maxlength/>$comment
	  	</td>
			</tr>";
    }

	function textSimple($name = "", $id = "", $value = "", $size = "", $maxlength = "", $type = "text") {
		if (!empty($name))
			$name = "name=\"$name\"";
		if (!empty($id))
			$id = "id=\"$id\"";
		if (!empty($size))
			$size = "size=\"$size\"";
		if (!empty($maxlength))
			$maxlength = "maxlength=\"$maxlength\"";
		$value = HTMLOutput::htmlencode($value);

		return $label . "<input type=\"$type\" $name $id value=\"$value\" $size $maxlength/>";
    }


	function password($label = "", $name = "", $id = "", $value = "", $size = "", $maxlength = "", $require = false) {
		if (!empty($name))
			$name = "name=\"$name\"";
		if (!empty($id))
			$id = "id=\"$id\"";
		if (!empty($size))
			$size = "size=\"$size\"";
		if (!empty($maxlength))
			$maxlength = "maxlength=\"$maxlength\"";
		if ($require)
			$label .= "&nbsp;<font color=\"#FF0000\">*</font>";

		$label .= "&nbsp;:&nbsp;";
		$value = HTMLOutput::htmlencode($value);

		return "<tr>
	  	<th>$label</th>
	  	<td>
	    <input type=\"password\" $name $id value=\"$value\" $size $maxlength/>
	  	</td>
			</tr>";
    }

	function passwordSimple($name = "", $id = "", $value = "", $size = "", $maxlength = "") {
		if (!empty($name))
			$name = "name=\"$name\"";
		if (!empty($id))
			$id = "id=\"$id\"";
		if (!empty($size))
			$size = "size=\"$size\"";
		if (!empty($maxlength))
			$maxlength = "maxlength=\"$maxlength\"";
		$value = HTMLOutput::htmlencode($value);

		return "<input type=\"password\" $name $id value=\"$value\" $size $maxlength/>";
    }

	function textArea($label = "", $name = "", $id = "", $col = 1, $row = 1, $value = "", $require = false) {
		if (!empty($name))
			$name = "name=\"$name\"";
		if (!empty($id))
			$id = "id=\"$id\"";
		if (!empty($col))
			$col = "cols=\"$col\"";
		if (!empty($row))
			$row = "rows=\"$row\"";
		if ($require)
			$label .= "&nbsp;<font color=\"#FF0000\">*</font>";
		$value = HTMLOutput::htmlencode($value);

		$label .= "&nbsp;:&nbsp;";
		return "<tr>
	  	<th>$label</th>
	  	<td>
	    <textarea $name $id $col $row>$value</textarea>
	  	</td>
			</tr>";
    }

	function dateTimeSimple($name, $date = "", $heure = "") {
		return "<input type=\"text\" name=\"$name" . "_date\" value=\"$date\" size=\"12\" maxlength=\"10\"/>" . _LB_TYPE_DATE .
				"&nbsp;<input type=\"text\" name=\"$name" . "_heure\" value=\"$heure\" size=\"8\" maxlength=\"5\"/>" . _LB_TYPE_HEURE;
    }

	function date($form = "", $label = "", $name = "", $id = "", $value = "", $size = "", $require = false, $time = false) {
		$obj = $name;
		if (!empty($name))
			$name = "name=\"$name\"";
		if (!empty($id))
			$id = "id=\"$id\"";
		if (!empty($size))
			$size = "size=\"$size\"";
		if ($require)
			$label .= "&nbsp;<font color=\"#FF0000\">*</font>";

		$value = HTMLOutput::datefr($value);

		$label .= "&nbsp;:&nbsp;";

		$time = $time ? "true" : "false";

		return "<tr>
	  	<th>$label</th>
	  	<td>
	    <input type=\"text\" $name $id value=\"$value\" $size/>
				<a href=\"javascript:$obj.popup();\"><img src=\"" . ADSERVER_URL . "/js/tigra/img/cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Click Here to Pick up the date\"/></a>
				<script type=\"text/javascript\" language=\"JavaScript\">
				var $obj = new calendar2( document.forms['$form'].elements['$obj'] );
				$obj.year_scroll = true;
				$obj.time_comp = $time;
				</script>
	  	</td>
			</tr>";
    }


	function dateSimple($form = "", $name = "", $id = "", $value = "", $size = "", $time = false) {
		$obj = $id;
		if (!empty($name))
			$name = "name=\"$name\"";
		if (!empty($id))
			$id = "id=\"$id\"";
		if (!empty($size))
			$size = "size=\"$size\"";

		$value = HTMLOutput::datefr($value);
		$time = $time ? "true" : "false";

		return "<input type=\"text\" $name $id value=\"$value\" $size/>
				<a href=\"javascript:$obj.popup( );\"><img src=\"" . ADSERVER_URL . "/js/tigra/img/cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Click Here to Pick up the date\"/></a>
				<script type=\"text/javascript\" language=\"JavaScript\">
				var $obj = new calendar2( document.forms[\"$form\"].elements[\"$obj\"] );
				$obj.year_scroll = true;
				$obj.time_comp = $time;
				</script>";
    }

	function select($label = "", $name = "", $id = "", $data = array(), $keyselected = 0, $size = "", $multiple = false, $js = "", $class = "Medium") {
		if (!empty($name)) {
			if ($size > 1)
				$name .= "[]";
			$name = "name=\"$name\"";
        }
		if (!empty($id))
			$id = "id=\"$id\"";
		if ($multiple)
			$multiple = "multiple";
        else
			$multiple = "";

		if ($size > 1) {
			$size = "size=" . $size;
        }
        else
			$size = "";

		if (!empty($label))
			$label .= "&nbsp;:&nbsp;";

		$str = "<tr>
	  	<th valign=\"top\">$label</th>
	  	<td>
	    <select $name $id $multiple $size class=\"$class\" $js>";
		if (is_array($data)) {
			foreach($data as $key => $value) {
				if($key > 0)//$value est pris de BD
					$value = HTMLOutput::htmlencode($value);
				if (is_array($keyselected)) {
					if (in_array($key, $keyselected))
						$flag_select = true;
                    else
						$flag_select = false;
                }
                else if ($key == $keyselected)
					$flag_select = true;
                else
					$flag_select = false;

				if ($flag_select)
					$str .= "<option value=\"$key\" selected=\"selected\">$value</option>";
                else
					$str .= "<option value=\"$key\">$value</option>";
            }
        }
		$str .= "</select>
	  	</td>
			</tr>";
		return $str;
    }

	function selectSimple($name = "", $id = "", $data = array(), $keyselected = 0, $size = "", $multiple = false, $js = "") {
		if (!empty($name))
			$name = "name=\"$name\"";
		if (!empty($id))
			$id = "id=\"$id\"";
		if ($multiple)
			$multiple = "multiple";
        else
			$multiple = "";
		if ($size != "")
			$size = "class=\"$size\"";
        else
			$size = "class=\"Small\"";

		$str = "<select $name $id $multiple $size $js>";
		foreach($data as $key => $value) {
			if($key > 0)//$value est pris de BD
				$value = HTMLOutput::htmlencode($value);
			if ($key == $keyselected)
				$str .= "<option value=\"$key\" selected=\"selected\">$value</option>";
            else
				$str .= "<option value=\"$key\">$value</option>";
        }
		$str .= "</select>";
		return $str;
    }

	function submit($align, $name, $value = "", $colspan = 2) {
		return "<tr>
	  <td colspan=\"$colspan\" align=\"$align\">
	    <input type=\"submit\" name=\"$name\" value=\"$value\"/>
	  </td>
				</tr>";
    }

	function submitSimple($name, $value = "") {
		return "<input type=\"submit\" name=\"$name\" value=\"$value\"/>";
    }

	function checkbox($label = "", $name = "", $value = "", $check = false, $disable = false, $text = "") {
		if (!empty($name))
			$name = "name=\"$name\"";
		if (!empty($value))
			$value = "value=\"$value\"";
		if ($check)
			$check = "checked=\"checked\"";
        else
			$check = "";
		if ($disable)
			$disable = "disabled=\"disabled\"";
        else
			$disable = "";

		$label .= "&nbsp;:&nbsp;";

		return "<tr>
	  	<th>$label</th>
	  	<td>
	    <input type=\"checkbox\" $name $value $check $disable />$text
	  	</td>
			</tr>";
    }



	function checkboxSimple($name = "", $value = "", $check = false, $disable = false, $label = "", $js = "") {
		if (!empty($name))
			$name = "name=\"$name\"";
		if (!empty($value))
			$value = "value=\"$value\"";
		if ($check)
			$check = "checked=\"checked\"";
        else
			$check = "";
		if ($disable)
			$disable = "disabled=\"disabled\"";
        else
			$disable = "";

		return "<input type=\"checkbox\" $name $value $check $disable $js/>$label";
    }

	function file($label, $name, $size = "", $comment = "") {
		if (!empty($size))
			$size = "size=\"$size\"";
		if (!empty($comment))
			$comment = "<br />$comment";

		return "<tr>
	  	<th>$label&nbsp;:&nbsp;</th>
	  	<td>
	    <input name=\"$name\" id=\"$name\" type=\"file\" $size/>$comment
	  	</td>
			</tr>";
    }

	function hidden($name, $value) {
		$value = HTMLOutput::htmlencode($value);
		return "<input type=\"hidden\" name=\"$name\" value=\"$value\"/>";
    }
	
	// create image with a tooltip
	function image_tooltip($id = "", $image = "", $image_size = IMAGE_SIZE_SMALL, $tooltip = "", $param = array()) {
		$image_path = ADSERVER_URL."/images/";
		$image_ext = ".".IMAGE_ICON_FILE_EXTENSION;
		$image_separator = "_";
		
		if(!empty($id))
			$id = "id=\"$id\"";

		if(!empty($image) && !empty($image_size)) {
			$image_src = $image_path.$image.$image_separator.$image_size.$image_ext;
			$src = "src=\"$image_src\"";
			$alt = "alt=\"$image$image_separator$image_size$image_ext\"";
		}
		elseif (!empty($image)) {
			$image_src = $image_path.$image.$image_ext;
			$src = "src=\"$image_src\"";
			$alt = "alt=\"$image.$image_ext\"";
		}

		if(!empty($tooltip) && !empty($param)) {
			foreach ($param as $name => $val) {
				if(is_file($tooltip) && file_exists($tooltip)) {
					$tooltip = str_replace("%".strtoupper($name)."%", $val, file_get_contents($tooltip));
				} else {
					$tooltip = str_replace("%".strtoupper($name)."%", $val, $tooltip);
				}
			}
			$tooltip = "onmouseover=\"return escape('$tooltip')\"";
		}
		elseif(!empty($tooltip))
			$tooltip = "onmouseover=\"return escape('$tooltip')\"";

		return "<img $id $src $alt $tooltip/>";
	}

	function startForm($name, $method, $action, $enctype = "") {
		if (!empty($enctype))
			$enctype = "enctype=\"$enctype\"";
		return "<form name=\"$name\" method=\"$method\" action=\"$action\" $enctype>";
    }

	function endForm() {
		return "</form>";
    }

	function startTable($class = "formInput", $id = "", $style = "") {
		if (!empty($class))
			$class = "class=\"$class\"";
		if (!empty($id))
			$id = "id=\"$id\"";

		return "<table $class $id $style>";
    }

	function endTable() {
		return "</table>";
    }


	//string number_format( float number [, int decimals [, string dec_point, string thousands_sep]] )
	function formatPrix($value, $currency = '', $empty = false) {
		if ($empty && $value == '') {
			return '';
        }
		$ret = number_format($value, 2, ', ', ' ');
		if ($currency) {
			$ret = $ret . '&nbsp;' . $currency;
        }

		return $ret;
    }

	function changeData2Combobox($name_function, $data1, $data2, $object2) {
		$str = "";
		$str .= "<script language=\"javascript\">";
		$str .= "function $name_function( chooser )";
		$str .= "{";
		$str .= "var data_obj1 = new Object( );";

		foreach($data1 as $key=>$value) {
			if (!is_array($data2[$key]))
				continue;

			$str .= "data_obj1[\"$key\"] = [";
			$i = 0;

			foreach($data2[$key] as $key2=>$value2) {
				$value2 = addslashes(utf8_encode($value2));

				if ($i == 0)
					$str .= "{value:\"$key2\", text:\"$value2\"}";
                else
					$str .= ",{value:\"$key2\", text:\"$value2\"}";

				$i++;
            }
			$str .= "];";
        }
		$str .= "
		var newElem;
		var where =( navigator.appName == \"Microsoft Internet Explorer\" )? -1 : null;
		var object2Chooser = chooser.form.elements[\"$object2\"];
		while( object2Chooser.options.length )
		{
			object2Chooser.remove( 0 );
		}
		var choice = chooser.options[chooser.selectedIndex].value;
		var db = data_obj1[choice];
		newElem = document.createElement( \"option\" );
		newElem.text = \"\";
		newElem.value = \"0\";
		object2Chooser.add( newElem, where );
		if( choice != \"\" )
		{
			for( var i = 0; i < db.length; i++  )
			{
				newElem = document.createElement( \"option\" );
				newElem.text = db[i].text;
				newElem.value = db[i].value;
				object2Chooser.add( newElem, where );
			}
		}";

		$str .= "}</script>";
		return $str;
    }

	// Afficher 0 dans ce cas il est vide
	function formatNb($nb) {
		if (!isset($nb) || $nb == '')
			return 0;
        else
			return $nb;
    }

	// Afficher le pourcentage
	function formatPourcentage($nb) {
		if (!isset($nb) || $nb == '')
			return 0 . '&nbsp;%';
        else
			return round($nb*100,2) . '&nbsp;%';
    }
}

?>
